package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import parser.WriterXML;

import entities.PedidoProveedores;
import entitiesdto.PedidoFabricacionDTO;
import entitiesdto.PedidoProveedoresDTO;

import businessdelegate.FacadeBusinessDelegate;

public class ComenzarFabricacionSVL extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
	public ComenzarFabricacionSVL() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		
		WriterXML writer = new WriterXML();
		
		PedidoProveedoresDTO pedidoProov = new PedidoProveedoresDTO();
		
		try {
			pedidoProov = FacadeBusinessDelegate.getInstance().comenzarFabricacion();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
		if (pedidoProov!=null){
			writer.generarPedidoProveedores(pedidoProov.getMateriaPrimaPedida(), "100", "1");
		}
		
		List<PedidoFabricacionDTO> pedidosFabr = new ArrayList<PedidoFabricacionDTO>();
		
		try {
			pedidosFabr = FacadeBusinessDelegate.getInstance().getPedidosFabricacion();
		} catch (NamingException e1) {
			e1.printStackTrace();
		}
		
//		List<PedidoProveedoresDTO> pedidosProov = new ArrayList<PedidoProveedoresDTO>();
		
//		try {
//			pedidosProov = FacadeBusinessDelegate.getInstance().getPedidosProveedores();
//		} catch (NamingException e1) {
//			e1.printStackTrace();
//		}
		
		session.setAttribute("pedidosFabricacion",pedidosFabr);
		
		session.setAttribute("pedidoProveedores",pedidoProov);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/comenzarFabricacion.jsp");
		
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}   
	
}